package views.games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.LineBorder;

import views.util.GameColors;
import core.constants.Constants;

public class GocceDiParoleView extends GameView implements ActionListener {


	private Timer cronometro = new Timer(1000, this);
	public JTextField input = new JTextField(30);
	private int timeMax = Constants.getGameTime() * 2;
	private ColoredLabel[] rows = new ColoredLabel[10];
	private MainPanel main;
	private JPanel coloumnPanel;
	private JPanel inputPanel;
	private JLabel[] countWord = new JLabel[10];
	private JLabel[] initialLetters = new JLabel[10];
	private Color defaultColorText;

	private GridBagConstraints gbc = new GridBagConstraints();
	private String initialWord;
	private JLabel iniziale;
	private int updateTime;
	private boolean update;
	private ActionListener currentActionListener;

	public GocceDiParoleView(String initialWord) {
		super(GameColors.BORDEAUX, GameColors.BORDEAUX_CHIARO, "1:00",
				"PUNTEGGIO: 0  ", "PAROLE DATE: 0");
		input.setFont(new Font("Segoe UI", Font.BOLD, 18));
		defaultColorText = input.getForeground();
		main = new MainPanel(new GridBagLayout());
		main.setBackground(GameColors.BORDEAUX);
		add(main, BorderLayout.CENTER);
		gbc.insets = new Insets(10, 5, 0, 0);
		gbc.gridx = 0;
		gbc.gridy = 0;
		iniziale = new JLabel(initialWord, JLabel.LEFT);
		iniziale.setFont(new Font("Segoe UI", Font.BOLD, 20));
		main.add(iniziale, gbc);

		gbc.gridx = 1;
		gbc.gridy = 0;

		input.setFont(new Font("Segoe UI", Font.BOLD, 20));
		input.setPreferredSize(new Dimension(160, 40));
		input.setBackground(GameColors.BORDEAUX_CHIARO);
		input.setBorder(new LineBorder(GameColors.BORDEAUX_CHIARO));
		input.setOpaque(true);
		main.add(input, gbc);

		gbc.gridx = 0;

		for (int i = 0; i <= 9; i++) {
			gbc.gridy = i + 1;
			JLabel numLetters = new JLabel("" + (4 + i));
			numLetters.setFont(new Font("Segoe UI", Font.BOLD, 20));
			main.add(numLetters, gbc);
			if (i == 9)
				numLetters.setText("13+");
		}

		gbc.gridx = 1;
		gbc.insets = new Insets(10, 15, 0, 0);
		for (int i = 0; i < 10; i++) {
			gbc.gridy = i + 1;
			rows[i] = new ColoredLabel();
			rows[i].setName(i + "");
			rows[i].setEnabled(false);
			rows[i].setOpaque(true);

			rows[i].setFont(new Font("Segoe UI", Font.BOLD, 20));
			rows[i].setPreferredSize(new Dimension(520, 30));
			main.add(rows[i], gbc);
		}

		gbc.gridx = 2;
		for (int i = 0; i < 10; i++) {
			gbc.gridy = i + 1;
			countWord[i] = new JLabel("0");
			countWord[i].setFont(new Font("Segoe UI", Font.BOLD, 20));
			main.add(countWord[i], gbc);
		}
		cronometro.start();
	}

	@Override
	public void gameEnded() {
		other.setVisible(false);
		gameClock.setVisible(false);
		main.removeAll();
		changeOvalPosition(-1);
		JLabel end = newLabel("Partita Conclusa!", GameColors.BORDEAUX_CHIARO);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 4;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(0, 0, 0, 0);
		main.add(end, gbc);
		gbc.gridy = 1;
		main.add(score, gbc);
		JButton restart = newButton("Ricomincia");
		JButton menu = newButton("Menu Giochi");
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		main.add(restart, gbc);
		gbc.gridx = 2;
		main.add(menu, gbc);
		restart.addActionListener(currentActionListener);
		menu.addActionListener(currentActionListener);
	}

	public static void main(String[] args) {
		JFrame f = new JFrame();
		f.add(new GocceDiParoleView("TRA"));
		f.pack();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}

	public boolean getUpdateStatus() {
		return update;
	}

	public void updated() {
		update = false;
		updateTime = 0;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		updateTime++;
		timeMax--;
		gameClock.setText("0:" + timeMax);
		if (updateTime >= 20)
			update = true;
		if (timeMax == 0)
			gameEnded();
	}

	public void setTextToRow(String text, int row) {
		rows[row].setText(text);
	}

	public void addActionListenerToGame(ActionListener al) {
		currentActionListener = al;
		input.addActionListener(al);

	}

	public void addKeyListenerToGame(KeyListener kl) {
		input.addKeyListener(kl);
	}

	public void incrementCountWord(int i) {
		if (i >= 13)
			i = 13; // se la parole ha 13 lettere o più incrementa la colonna
					// 13+
		int currentValue = Integer.parseInt(countWord[i - 4].getText());
		System.out.println("Grandezza: " + currentValue);
		currentValue++;
		countWord[i - 4].setText(currentValue + "");
		input.setText("");
	}

	public void showError() {
		input.setForeground(Color.RED);

	}

	public void removeError() {
		input.setForeground(defaultColorText);
	}

	public void showRipetition() {
		input.setForeground(Color.YELLOW);

	}

	public void setInitialWord(String initialWord) {
		this.initialWord = initialWord;
	}

	public void setGameScore(int newScore, int completeWord) {
		score.setText("PUNTEGGIO: " + newScore);
		other.setText("PAROLE DATE: " + completeWord);
	}

	public void refresh(String firstString) {
		initialWord = firstString;
		iniziale.setText(firstString);

	}

	private int currentOvalPosition;

	public int getCurrentOvalPosition() {
		return currentOvalPosition;
	}

	public void changeOvalPosition(int n) {
		currentOvalPosition = n;
		main.setOvalPosition(currentOvalPosition);
		main.repaint();
	}

	public void incrementRow(int i) {
		rows[i].setWidth(rows[i].getColoredWidth() + 53);
	}

	private class MainPanel extends JPanel {
		private int Y = 120;

		public MainPanel(LayoutManager lm) {
			super(lm);
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.BLUE);
			g.fillOval(88, Y, 50, 50);
		}
		
		public void setOvalPosition(int n){
			switch(n){
			case -1: Y = 1000; break;  //DA MIGLIORARE TODO
			case 0: Y = 120; break;  //4 lettere o 3
			case 1: Y = 150; break;  //5 
			case 2: Y = 190; break;  //6
			case 3: Y = 230; break;  //7
			case 4: Y = 270; break;  //8 
			case 5: Y = 310; break;  //9
			case 6: Y = 350; break;  //10
			case 7: Y = 390; break;  //11
			case 8: Y = 430; break;  //12
			case 9: Y = 470; break;  //13+
			}
		}

	}

	private class ColoredLabel extends JLabel {

		private int currentWidth;
		private int currentHeight;

		public ColoredLabel() {
			super();
			currentWidth = 0;
			currentWidth = 0;
		}

		@Override
		public void setPreferredSize(Dimension dim) {
			super.setPreferredSize(dim);
			currentHeight = (int) dim.getHeight();
		}

		public void setWidth(int width) {
			currentWidth = width;
			repaint();
		}

		public int getColoredWidth() {
			return currentWidth;
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.ORANGE);
			g.fillRect(0, 0, currentWidth, currentHeight);
		}

	}

}
